import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.net.MalformedURLException;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import remotecontrol.RandomJukeMain;
import Model.PlayerModel;
import View.PlayerView;
import datalayer.DBobject;
 
public class LoadingScreen extends JFrame{
 
	private static final long serialVersionUID = 1L;
		private static LoadingScreen instance;
        private JDialog dialog;
        private long balance;
        private static JFrame frame = new JFrame();
        private JPanel contentPane = new JPanel();
        
        public long getBalance() {
                return balance;
        }
       
        public static LoadingScreen getInstance() {
                if (instance == null) {
                        instance = new LoadingScreen();
                }
                return instance;
        }      
 
        public void setBalance(long balance) {
                this.balance = balance;
        }
 
        public String getUsername() {
                return username;
        }
 
        public void setUsername(String username) {
                this.username = username;
               
        }
 
        
        private String username;
       
        protected void initUI() throws MalformedURLException {
                showSplashScreen();
                SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
 
                        @Override
                        protected Void doInBackground() throws Exception {
                        				new RandomJukeMain();
                                        DBobject db = new DBobject();
                                		db.initDB();
                                    
                                return null;
                        }
 

                        @Override
                        protected void done() {
                                try {
									showFrame();
								} catch (ClassNotFoundException e) {
									
								} catch (InstantiationException e) {
								
								} catch (IllegalAccessException e) {
									
								} catch (UnsupportedLookAndFeelException e) {
									
								}
                                hideSplashScreen();                    
                        }
                };
                worker.execute();
        }
 
        public static void hideframe(){
                frame.setVisible(false);               
        }
        protected void hideSplashScreen() {
                dialog.setVisible(false);
                dialog.dispose();
        }
 
        protected void showSplashScreen() throws MalformedURLException {
        		dialog = new JDialog((Frame) null);
                dialog.setModal(false);
                dialog.setUndecorated(true);
                dialog.setBackground(new Color(0.0f,0.0f,0.0f,0.0f));
                JLabel background = new JLabel(new ImageIcon(
                                ImageIcon.class.getResource("/loading.png")));
                background.setLayout(new BorderLayout());
 
                dialog.getContentPane().add(background);
                dialog.pack();
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
        }
 
        protected void showFrame() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException  {
        	try {
    			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    		} catch (ClassNotFoundException | InstantiationException
    				| IllegalAccessException | UnsupportedLookAndFeelException e) {
    			
    		}
        	
    		PlayerView view = new PlayerView();
    		view.setVisible(true);
    		PlayerModel.getInstance().updateListModel();
        }
 
        public void main(String[] args) throws ClassNotFoundException,
                        InstantiationException, IllegalAccessException,
                        UnsupportedLookAndFeelException {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                SwingUtilities.invokeLater(new Runnable() {
 
                        @Override
                        public void run() {
 
                                try {
                                        new LoadingScreen().initUI();
                                } catch (MalformedURLException e) {
                               
                                }
                        }
                });
        }
 
 
        public JFrame getFrame() {
                return frame;
        }
 
        public void setFrame(JFrame frame) {
                LoadingScreen.frame = frame;
        }
 
        public JPanel getContentPane() {
                return contentPane;
        }

        public void setContentPane(JPanel contentPane) {
                this.contentPane = contentPane;
        }
}
